package com.rose.mall.product.thread;

import com.rose.common.utils.R;
import org.junit.Test;

import java.util.concurrent.*;

public class ThreadTest {

    public static ExecutorService executorService = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main --- start");
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(()->{
            System.out.println("当前线程"+Thread.currentThread().getId());
            int i  = 10 / 2;
            System.out.println("运行结果"+i);
        },executorService);
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果" + i);
//            return i;
//        }, executorService).whenComplete((result,exception)->{
//            System.out.println("异步任务完成了+ 结果是"+result+"异常是"+exception);
//        }).exceptionally((exception)->{
//            //可以感知异常，可以修改数据
//            return 10;
//        });
//
//
        //  使用handle方法
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果" + i);
            return i;
        }, executorService).handle((res,exception)->{
            if (res != null){
                return res*2;
            }
            if (exception!= null){
                return 0;
            }
            return 0;
        });


        CompletableFuture.supplyAsync(()->{
            System.out.println("当前线程" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果" + i);
            return i;
        },executorService).thenAcceptAsync(res->{
            System.out.println("任务2启动了");
        });

        System.out.println(future.get());
        System.out.println("main --- end       ");
    }
    @Test
    public  void thread() throws ExecutionException, InterruptedException {
        System.out.println("main --- start");
        System.out.println("当前线程"+Thread.currentThread().getId());
        // 继承thread开启一个线程
        // Thread01 thread01 = new Thread01();
        // thread01.start();

        //========第二种方式
        Runable01 runable01 = new Runable01();
        new Thread(runable01).start();


        // ======第三种方式
        // Callable01 callable01 = new Callable01();
        // FutureTask<Integer> integerFutureTask = new FutureTask<>(callable01);
        // new Thread(integerFutureTask).start();

        //等待整个线程执行完成 获取返回结果
        //Integer integer = integerFutureTask.get();

        //每个系统中只有一两个线程池。每个异步任务，都提交给线程池

//
        System.out.println("main --- end       ");


    }

    public static class Thread01 extends Thread{
        @Override
        public void run(){
            System.out.println("当前线程"+Thread.currentThread().getId());
            int i  = 10 / 2;
            System.out.println("运行结果"+i);
        }
    }
    public static class Runable01 implements Runnable{
        @Override
        public void run(){
            System.out.println("当前线程"+Thread.currentThread().getId());
            int i  = 10 / 2;
            System.out.println("运行结果"+i);
        }
    }
    public static class Callable01 implements Callable<Integer> {
        @Override
        public Integer call(){
            System.out.println("当前线程"+Thread.currentThread().getId());
            int i  = 10 / 2;
            System.out.println("运行结果"+i);
            return i;
        }
    }
}
